package top.lgd.mapper.study;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.lgd.pojo.entity.study.Record;
import top.lgd.pojo.res.study.CatalogDurationRes;
import top.lgd.pojo.res.study.DayDurationRes;

import java.util.List;

/**
 * 学习记录表i Mapper 接口
 *
 * @author YYJ
 * @version 1.0
 * @since 2023/12/15 10:10
 */
@Mapper
public interface RecordMapper extends BaseMapper<Record> {

    /**
     * @param userId
     * @param startDate
     * @return
     */
    @Select("SELECT DATE_FORMAT(end_time,'%Y-%m-%d') study_day, sum(duration) duration, sum(current_index) current_index FROM study_record " +
            "WHERE end_time >= '${startDate}' AND data_user_id = ${userId} GROUP BY DATE_FORMAT(end_time,'%Y-%m-%d')")
    List<DayDurationRes> queryDurationList(@Param("userId") Long userId, @Param("startDate") String startDate);

    /**
     * @param userId
     * @return
     */
    @Select("SELECT ca.name type, duration , counter FROM (SELECT root_catalog_id catalog_id, SUM(duration)/60 duration, COUNT(id) counter " +
            "FROM study_record WHERE data_user_id = ${userId} GROUP BY root_catalog_id ) AS re " +
            "LEFT JOIN study_catalog ca ON re.catalog_id = ca.id ORDER BY duration DESC")
    List<CatalogDurationRes> catalogDurationList(@Param("userId") Long userId);

    /**
     * @param userId
     * @param startDate
     * @param endDate
     * @return
     */
    @Select("SELECT sum(duration) duration FROM study_record " +
            "WHERE end_time >= '${startDate}' AND end_time < '${endDate}' AND data_user_id = ${userId}")
    Integer sumDuration(@Param("userId") Long userId, @Param("startDate") String startDate, @Param("endDate") String endDate);
}